'use strict';

angular.module('myApp.teachers', [])
.controller('teachersCtrl', ['$scope','$routeParams','$http','$location','breadcrumbData',
function($scope,$routeParams,$http,$location,$breadcrumbData){

	var column = 'subject_id';

	//筛选类型
    $('#search_input').hide();
    $scope.selectTypes = [
    	{id:'1',name:'所教科目'},
    	{id:'2',name:'姓名'}
    ];
    $scope.selectType = $scope.selectTypes[0].id;
    $scope.update = function() {
		if($scope.selectType == "1"){
			$('#search_select2').show();
			$('#search_input').hide();
			column = 'subject_id';
		}else{
			$('#search_select2').hide();
			$('#search_input').show();
			column = 'name';
		};
	}

	//获取所有科目
    $http.get('v1/subject').success(function(data){
    	$scope.selectClasses = [];
    	var data = data.result;
    	for (var i = 0; i < data.length; i++) {
    		$scope.selectClasses.push({'id':data[i].id,'name':data[i].name});
    	};
    	$scope.selectClass = $scope.selectClasses[0].id;

    	var content = $scope.selectClass;
    	$scope.update2 = function(){
	    	content = $scope.content;
	    }
	    $scope.update3 = function(){
	    	content = $scope.selectClass;
	    }
	    //搜索
		$scope.search = function(){
			$http({
			    url: 'v1/teacher', 
			    method: "GET",
			    params: {column: column,content:content}
			 }).success(function(data){
	            if(!data.error ){
	            	var temp_result = data.result.data;
	            	for( var i=0; i<temp_result.length ; i++ ){
		            	if( temp_result[i].sex == '1' )
							temp_result[i].sex = '男';
						else
							temp_result[i].sex = '女';
					}

	 				$scope.teachers = data.result['data'];           	
	            }else{
	                alert(data.message);
	            }
	 		});
			
		}
    })
    
    
   
	//显示所有老师
	$http.get('v1/teacher').success(function(data){
			var temp_result = data.result['data'];
			for( var i=0; i<temp_result.length ; i++ ){
				if( temp_result[i].sex == 1 )
					temp_result[i].sex = '男';
				else
					temp_result[i].sex = '女';
			}
			$scope.teachers = temp_result;
	});

	

	//显示全部
	$scope.reload = function(){
		$http.get('v1/teacher').success(function(data){
			var temp_result = data.result['data'];
			for( var i=0; i<temp_result.length ; i++ ){
				if( temp_result[i].sex == 1 )
					temp_result[i].sex = '男';
				else
					temp_result[i].sex = '女';
			}
			$scope.teachers = data.result['data'];
		});		
	};

	//跳转到新建
    $scope.jumpToUrl = function(path) {
        $location.path(path);
    };

	//删除记录
	$scope.delete = function(teacher){
		$http.delete('v1/teacher/'+ teacher.id).success(function(data){
			if( !data.error ){
				$scope.teachers.splice($scope.teachers.indexOf(teacher),1);
				alert("删除成功","success");
			}else{
				alert(data.message);
			}
		});
	};
	

}]);
